package erfgame.frontend.desktop.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;

public abstract class AbstractView {
	
	private Collection<ViewActionListener> listeners = new ArrayList<ViewActionListener>();
	
	protected void fireViewAction( int actionType ) {
		this.fireViewAction( actionType, null );
	}
	
	protected void fireViewAction( int actionType, Object parameter ) {
		for( ViewActionListener listener : this.listeners ) {
			listener.actionPerformed( this, actionType, parameter );
		}
	}
	
	public void addViewActionListener( ViewActionListener listener ) {
		this.listeners.add( listener );
	}
	
	public void removeViewActionListener( ViewActionListener listener ) {
		this.listeners.remove( listener );
	}
	
	protected ActionListener createFireViewActionListener( int actionType ) {
		return new FireViewActionListener( actionType );
	}
	
	class FireViewActionListener implements ActionListener {
		private int actionType;
		
		public FireViewActionListener( int actionType ) {
			this.actionType = actionType;
		}
		
		public void actionPerformed(ActionEvent e) {
			fireViewAction( this.actionType );
		}
	}
}
